package com.live.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.live.adapter.LiveNewsSA;
import com.live.common.Constant;
import com.live.common.ImageDownloader;
import com.live.common.LiveCommon;
import com.live.task.LiveFriendCheck;
import com.live.task.LiveTopic;

public class LiveFriendHomeActivity extends LiveBaseActivity {

	private LiveNewsSA newsSA;

	private ListView newsLV;

	private View liveFoot;

	private int page = 0;

	private int lastIndex = 0;

	private List<Map<String, Object>> newsList = new ArrayList<Map<String, Object>>();

	private final ImageDownloader imageDownloader = new ImageDownloader();

	private Bundle bundle;

	private String uId;

	private String fId;

	private Button friendFuncBtn;

	protected void onResume() {
		super.onResume();

		// 当登录后，如果是本人，就进入到自己的页面
		if (fId != null && fId.equals(store.getString("id", null))) {
			// 进入自己的页面
			Intent intent = new Intent();
			intent.setClass(this, LiveUserHomeActivity.class);
			this.startActivity(intent);
			// 关闭当前画面
			this.finish();
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_friend_home);

		/* 获取Intent中的Bundle对象 */
		bundle = this.getIntent().getExtras();

		uId = store.getString("id", null);
		fId = bundle.getString("uId");

		// 设置头部信息
		LiveCommon.setHead(context, (LinearLayout) findViewById(R.id.liveHead), R.string.title_friend_area,
				R.string.reload_btn);

		// 设置头部按钮（刷新）
		Button reloadBtn = (Button) findViewById(R.id.headFuncBtn);
		reloadBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bundle.putInt("page", 0);
				bundle.putString("uId", fId);
				LiveTopic task = new LiveTopic(context, newsList, newsSA, null, Constant.ACTION_SELECT, newsLV,
						liveFoot);
				task.execute(bundle);
			}
		});

		// 好友主页
		TextView userId = (TextView) findViewById(R.id.userId);
		userId.setText(bundle.getString("userId"));

		// 设置头像
		ImageView userPic = (ImageView) findViewById(R.id.userPic);
		if (Constant.Y.equals(bundle.getString("userPic"))) {
			String imageUrl = Constant.SITE_URL + Constant.SITE_AVATAR_PATH + bundle.getString("uId") + ".jpg";
			imageDownloader.download(imageUrl, userPic, bundle.getString("userPicVer"));
		}

		// 设置城市
		TextView userCity = (TextView) findViewById(R.id.userCity);
		if (!"".equals(bundle.getString("userCity"))) {
			userCity.setText(bundle.getString("userCity"));
		} else {
			userCity.setText(R.string.user_city_no);
		}

		// 设置性别
		TextView userSex = (TextView) findViewById(R.id.userSex);
		if (Constant.USER_SEX_MAN.equals(bundle.getString("userSex"))) {
			userSex.setText("♂");
			userSex.setTextColor(0xff00CCFF);
		} else {
			userSex.setText("♀");
			userSex.setTextColor(0xffFF6699);
		}

		// 设置年龄
		TextView userAge = (TextView) findViewById(R.id.userAge);
		userAge.setText(bundle.getString("userAge"));

		// 设置签名
		TextView userSign = (TextView) findViewById(R.id.userSign);
		if ("".equals(bundle.getString("userSign"))) {
			userSign.setText(R.string.user_sign_no);
		} else {
			userSign.setText(bundle.getString("userSign"));
		}

		// 取得新鲜事VIEW
		newsLV = (ListView) findViewById(R.id.newsList);

		// 为新鲜事添加“读取更多按钮”
		liveFoot = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.live_foot, null, false);
		// 设置新鲜事
		newsSA = new LiveNewsSA(context, newsList, R.layout.topic_list, new String[] {}, new int[] {}, store.getString(
				"userId", null));
		newsLV.addFooterView(liveFoot);
		newsLV.setAdapter(newsSA);
		newsLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Map<String, Object> obj = newsList.get(arg2);

				Intent intent = new Intent();
				intent.setClass(context, LiveReplayViewActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("id", (String) obj.get("id"));
				bundle.putString("uId", (String) obj.get("uId"));
				bundle.putString("userId", (String) obj.get("userId"));
				bundle.putString("userPic", (String) obj.get("userPic"));
				bundle.putString("userPicVer", (String) obj.get("userPicVer"));
				bundle.putString("userSex", (String) obj.get("userSex"));
				bundle.putString("userCity", (String) obj.get("userCity"));
				bundle.putString("userAge", (String) obj.get("userAge"));
				bundle.putString("title", (String) obj.get("title"));
				bundle.putString("content", (String) obj.get("content"));
				bundle.putString("updateDate", (String) obj.get("updateDate"));
				intent.putExtras(bundle);
				startActivityForResult(intent, Constant.REQUEST_CODE);
			}
		});
		newsLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && lastIndex == newsSA.getCount()) {
					page = page + 1;
					Bundle bundle = new Bundle();
					bundle.putInt("page", page);
					bundle.putString("uId", fId);
					LiveTopic task = new LiveTopic(context, newsList, newsSA, null, Constant.ACTION_SELECT, newsLV,
							liveFoot);
					task.execute(bundle);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				lastIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});

		// 初始化默认读取新鲜事
		Bundle bundle = new Bundle();
		bundle.putString("self", "1");
		bundle.putString("uId", fId);
		bundle.putInt("page", 0);
		LiveTopic task = new LiveTopic(context, newsList, newsSA, null, Constant.ACTION_SELECT, newsLV, liveFoot);
		task.execute(bundle);

		friendFuncBtn = (Button) findViewById(R.id.friendFuncBtn);
		// 绿豆粥 id=3
		if (!"3".equals(fId)) {
			Bundle bundle2 = new Bundle();
			bundle2.putString("uId", uId);
			bundle2.putString("fId", fId);
			LiveFriendCheck task2 = new LiveFriendCheck(context, friendFuncBtn);
			task2.execute(bundle2);
		} else {
			// 对于“绿豆粥”特殊设置发送消息按钮
			friendFuncBtn.setVisibility(Constant.VISIBLE);
			friendFuncBtn.setText(R.string.message_btn);
			friendFuncBtn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(context, LiveMessageActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("uId", uId);
					bundle.putString("fId", fId);
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			});
		}

		/*
		 * friendFuncBtn = (Button) findViewById(R.id.friendFuncBtn);
		 * friendFuncBtn.setVisibility(Constant.VISIBLE); if
		 * (Constant.ACTION_ALLOW.equals(bundle.getString("f"))) { // 设置发送消息按钮
		 * friendFuncBtn.setText(R.string.message_btn);
		 * friendFuncBtn.setOnClickListener(new View.OnClickListener() { public
		 * void onClick(View v) { Intent intent = new Intent();
		 * intent.setClass(context, LiveMessageActivity.class); Bundle bundle =
		 * new Bundle(); bundle.putString("uId", uId); bundle.putString("fId",
		 * fId); intent.putExtras(bundle); startActivity(intent); } }); } else
		 * if (Constant.ACTION_APPLY.equals(bundle.getString("f"))) {
		 * friendFuncBtn.setText(R.string.check_btn);
		 * friendFuncBtn.setBackgroundResource(R.drawable.gray_func_btn);
		 * friendFuncBtn.setPadding(3, 0, 0, 3); } else { // 设置关注好友按钮
		 * friendFuncBtn.setText(R.string.apply_btn);
		 * friendFuncBtn.setOnClickListener(new View.OnClickListener() { public
		 * void onClick(View v) { if (uId != null) { // 用户已登录 String url =
		 * Constant.SITE_URL + "live_friend.php?locale=" +
		 * LiveTools.getLocaleLanguage() + "&"; url += "fUId=" + fId + "&uId=" +
		 * store.getString("id", null) + "&fState=" + Constant.ACTION_APPLY;
		 * Bundle bundle = new Bundle(); bundle.putString("action",
		 * Constant.ACTION_APPLY); LiveFriend task = new LiveFriend(context,
		 * url, null, null, friendFuncBtn, null, null, null);
		 * task.execute(bundle); } else { // 用户未登录 Intent intent = new Intent();
		 * intent.setClass(context, LiveUserLoginActivity.class);
		 * context.startActivity(intent); } } }); }
		 */

	}
}